package com.ad.pet.utils;

import android.text.TextUtils;

import com.ad.pet.Logger;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 事件格式化工具
 * Created by dell - on 2017/11/2.
 */

public class TimeFormatUtil {
    private static final String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
    private static final String DATE_TIME_PATTERN_1 = "MM.dd";
    private static final String DATE_TIME_PATTERN_2 = "yyyy年MM月dd日";
    private static final String DATE_TIME_PATTERN_3 = "yyyy年MM月";
    private static final String DATE_TIME_PATTERN_4 = "yyyy-MM-dd";
    private static final String DATE_TIME_PATTERN_5 = "MM月dd日";
    private static final String DATE_TIME_PATTERN_6 = "yyyy-MM";
    private static final String DATE_TIME_PATTERN_7 = "yyyy/MM/dd";
    private static final String DATE_TIME_PATTERN_8 = "yyyy";
    private static final String DATE_TIME_PATTERN_9 = "yyyy/MM";
    private static final String DATE_TIME_PATTERN_10 = "MM月dd日 HH:mm";

    private static SimpleDateFormat sFormat = new SimpleDateFormat(DATE_TIME_PATTERN);

    /**
     * 2017年10月11日
     *
     * @param date
     * @return
     */
    public static String getYYMMDD(String date) {
        date = TextUtils.isEmpty(date) ? "" : date;
        String result = "";
        try {
            sFormat.applyPattern(DATE_TIME_PATTERN);
            Date time = sFormat.parse(date);
            sFormat.applyPattern(DATE_TIME_PATTERN_2);
            result = sFormat.format(time);
        } catch (ParseException e) {
//            e.printStackTrace();
            Logger.d("getYYMMDD 日期格式化错误 " + date);
        }
        return result;
    }

    /**
     * 10.11
     *
     * @param date
     * @return
     */
    public static String getMMDD(String date) {
        date = TextUtils.isEmpty(date) ? "" : date;
        String result = "";
        try {
            sFormat.applyPattern(DATE_TIME_PATTERN);
            Date time = sFormat.parse(date);
            sFormat.applyPattern(DATE_TIME_PATTERN_1);
            result = sFormat.format(time);
        } catch (ParseException e) {
//            e.printStackTrace();
            Logger.d("getMMDD 日期格式化错误 " + date);
        }
        return result;
    }

    /**
     * 2017-10-11
     *
     * @param date
     * @return
     */
    public static String getYY_MM_DD(String date) {
        date = TextUtils.isEmpty(date) ? "" : date;
        String result = "";
        try {
            sFormat.applyPattern(DATE_TIME_PATTERN);
            Date time = sFormat.parse(date);
            sFormat.applyPattern(DATE_TIME_PATTERN_4);
            result = sFormat.format(time);
        } catch (ParseException e) {
//            e.printStackTrace();
            Logger.d("getYY_MM_DD 日期格式化错误 " + date);

        }
        return result;
    }

    /**
     * 2017年10月
     *
     * @param date
     * @return
     */
    public static String getYYMM(String date) {
        date = TextUtils.isEmpty(date) ? "" : date;
        String result = "";
        try {
            sFormat.applyPattern(DATE_TIME_PATTERN);
            Date time = sFormat.parse(date);
            sFormat.applyPattern(DATE_TIME_PATTERN_3);
            result = sFormat.format(time);
        } catch (ParseException e) {
//            e.printStackTrace();
            Logger.d("getYYMM 日期格式化错误 " + date);
        }
        return result;
    }

    /**
     * 10月11日
     *
     * @param date
     * @return
     */
    public static String getTextMMDD(String date) {
        date = TextUtils.isEmpty(date) ? "" : date;
        String result = "";
        try {
            sFormat.applyPattern(DATE_TIME_PATTERN);
            Date time = sFormat.parse(date);
            sFormat.applyPattern(DATE_TIME_PATTERN_5);
            result = sFormat.format(time);
        } catch (ParseException e) {
//            e.printStackTrace();
            Logger.d("getTextMMDD 日期格式化错误 " + date);
        }
        return result;
    }

    /**
     * 2017
     *
     * @param date
     * @return
     */
    public static String getYear(String date) {
        date = TextUtils.isEmpty(date) ? "" : date;
        String result = "";
        try {
            sFormat.applyPattern(DATE_TIME_PATTERN);
            Date time = sFormat.parse(date);
            sFormat.applyPattern("yyyy");
            result = sFormat.format(time);
        } catch (Exception e) {
//            e.printStackTrace();
            Logger.d("getYear 日期格式化错误 " + date);
        }
        return result;
    }

    /**
     * 2017年
     *
     * @param date
     * @return
     */
    public static String getTextYear(String date) {
        date = TextUtils.isEmpty(date) ? "" : date;
        String result = "";
        try {
            sFormat.applyPattern(DATE_TIME_PATTERN);
            Date time = sFormat.parse(date);
            sFormat.applyPattern("yyyy年");
            result = sFormat.format(time);
        } catch (ParseException e) {
//            e.printStackTrace();
            Logger.d("getTextYear 日期格式化错误 " + date);
        }
        return result;
    }

    /**
     * 2017/11/11
     *
     * @param date
     * @return
     */
    public static String getYYiMMiDD(String date) {
        date = TextUtils.isEmpty(date) ? "" : date;
        String result = "";
        try {
            sFormat.applyPattern(DATE_TIME_PATTERN);
            Date time = sFormat.parse(date);
            sFormat.applyPattern("yyyy/MM/dd");
            result = sFormat.format(time);
        } catch (Exception e) {
//            e.printStackTrace();
            Logger.d("getYYiMMiDD 日期格式化错误 " + date);
        }
        return result;
    }

    /**
     * 2017/11
     *
     * @param date
     * @return
     */
    public static String getYYiMM(String date) {
        date = TextUtils.isEmpty(date) ? "" : date;
        String result = "";
        try {
            sFormat.applyPattern(DATE_TIME_PATTERN);
            Date time = sFormat.parse(date);
            sFormat.applyPattern("yyyy/MM");
            result = sFormat.format(time);
        } catch (ParseException e) {
            Logger.d("getYYiMM 日期格式化错误 " + date);
//            e.printStackTrace();
        }
        return result;
    }

    /**
     * 2017-11
     *
     * @param date yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String getCircularDate(String date) {
        date = TextUtils.isEmpty(date) ? "" : date;
        String result = "";
        try {
            sFormat.applyPattern(DATE_TIME_PATTERN_6);
            Date time = sFormat.parse(date);
            sFormat.applyPattern("yyyy年MM月");
            result = sFormat.format(time);
        } catch (ParseException e) {
//            e.printStackTrace();
            Logger.d("getCircularDate 日期格式化错误 " + date);
        }
        return result;
    }


    /**
     * 将日期变成上传的日期格式
     *
     * @param date 2017/11/12
     * @return yyyy-MM-dd HH:mm:ss
     */
    public static String getStanardDateYear(String date) {
        date = TextUtils.isEmpty(date) ? "" : date;
        String result = "";
        try {
            sFormat.applyPattern(DATE_TIME_PATTERN_8);
            Date time = sFormat.parse(date);
            sFormat.applyPattern(DATE_TIME_PATTERN);
            result = sFormat.format(time);
        } catch (ParseException e) {
//            e.printStackTrace();
            Logger.d("getStanardDate 日期格式化错误 " + date);
        }
        return result;
    }

    /**
     * 将日期变成上传的日期格式
     *
     * @param date 2017/11/12
     * @return yyyy-MM-dd HH:mm:ss
     */
    public static String getStanardDateMonth(String date) {
        date = TextUtils.isEmpty(date) ? "" : date;
        String result = "";
        try {
            sFormat.applyPattern(DATE_TIME_PATTERN_9);
            Date time = sFormat.parse(date);
            sFormat.applyPattern(DATE_TIME_PATTERN);
            result = sFormat.format(time);
        } catch (ParseException e) {
//            e.printStackTrace();
            Logger.d("getStanardDate 日期格式化错误 " + date);
        }
        return result;
    }

    public static String dateToStamp(String s) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_TIME_PATTERN_7);
        Date date = null;
        try {
            date = simpleDateFormat.parse(s);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long ts = date.getTime();
        res = String.valueOf(ts);
        return res;
    }

    public static String stampToDate(long s){
        Date d = new Date(s);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_TIME_PATTERN_7);
        String t = simpleDateFormat.format(d);

        return t;
    }
    /**
     * 将日期变成上传的日期格式
     *
     * @param date 2017/11/12
     * @return yyyy-MM-dd HH:mm:ss
     */
    public static String getStanardDate(String date) {
        date = TextUtils.isEmpty(date) ? "" : date;
        String result = "";
        try {
            sFormat.applyPattern(DATE_TIME_PATTERN_7);
            Date time = sFormat.parse(date);
            sFormat.applyPattern(DATE_TIME_PATTERN);
            result = sFormat.format(time);
        } catch (ParseException e) {
//            e.printStackTrace();
            Logger.d("getStanardDate 日期格式化错误 " + date);
        }
        return result;
    }

    /**
     * @param date 计算距离今天还有多少天
     */
    public static String getRemindingDays(String date) {
        if (TextUtils.isEmpty(date)) {
            return "0";
        }
        String result = "";
        try {
            sFormat.applyPattern(DATE_TIME_PATTERN);
            Date time = sFormat.parse(date);
            long s1 = time.getTime();//将时间转为毫秒
            long s2 = System.currentTimeMillis();//得到当前的毫秒
            int day = (int) ((s2 - s1) / 1000 / 60 / 60 / 24);
            if (day <= 0) {
                result = "0";
            }
        } catch (Exception e) {
            Logger.d("getRemindingDays 日期格式化错误 " + date);
            return "0";
        }
        return result;
    }


    static String[][] constellations = {{"摩羯座", "水瓶座"}, {"水瓶座", "双鱼座"}, {"双鱼座", "白羊座"}, {"白羊座", "金牛座"}, {"金牛座", "双子座"}, {"双子座", "巨蟹座"}, {"巨蟹座", "狮子座"},
            {"狮子座", "处女座"}, {"处女座", "天秤座"}, {"天秤座", "天蝎座"}, {"天蝎座", "射手座"}, {"射手座", "摩羯座"}};
    //星座分割时间
    static int[] date = {20, 19, 21, 20, 21, 22, 23, 23, 23, 24, 23, 22};
    static String[] constells = {"白羊座", "金牛座", "双子座", "巨蟹座", "狮子座", "处女座", "天秤座", "天蝎座", "射手座", "摩羯座", "水瓶座", "双鱼座"};
    static String[] constellsIds = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"};

    //星座生成 传进是日期格式为: yyyy/mm/dd
    public static String getConstellations(String birthday) {
        String[] data = birthday.split("/");
        int day = date[Integer.parseInt(data[1]) - 1];
        String[] cl1 = constellations[Integer.parseInt(data[1]) - 1];
        if (Integer.parseInt(data[2]) >= day) {
            return cl1[1];
        } else {
            return cl1[0];
        }
    }

    /**
     * 根据星座名获取id
     *
     * @param cons
     * @return
     */
    public static String getConstellId(String cons) {
        String s = "";
        for (int i = 0; i < constells.length; i++) {
            if (cons.equals(constells[i])) {
                s = constellsIds[i];
            }
        }
        return s;
    }


    /**
     * 10月11日 22:00
     *
     * @param date
     * @return
     */
    public static String getTextMMDDhhpp(String date) {
        date = TextUtils.isEmpty(date) ? "" : date;
        String result = "";
        try {
            sFormat.applyPattern(DATE_TIME_PATTERN);
            Date time = sFormat.parse(date);
            sFormat.applyPattern(DATE_TIME_PATTERN_10);
            result = sFormat.format(time);
        } catch (ParseException e) {
//            e.printStackTrace();
            Logger.d("getTextMMDD 日期格式化错误 " + date);
        }
        return result;
    }


    /**
     * 评论时间线
     *
     * @param date
     * @return
     */
    public static String getTextCommentTime(String date) {
        date = TextUtils.isEmpty(date) ? "" : date;
        String result = "";
        try {
            sFormat.applyPattern(DATE_TIME_PATTERN);
            Date time = sFormat.parse(date);
            long creatTime = time.getTime();
            long nowTime = System.currentTimeMillis();
            long cutTime = nowTime - creatTime;
            if (cutTime <= 60 * 1000) {
                result = "刚刚";
            } else if (cutTime > 60L * 1000 && cutTime <= 60L * 60 * 1000) {
                result = ((int) (cutTime / 60 / 1000)) + "分钟前";
            } else if (cutTime > 60L * 60 * 1000 && cutTime <= 60L * 60 * 24 * 1000) {
                result = ((int) (cutTime / 60 / 60 / 1000)) + "小时前";
            } else if (cutTime > 60L * 60 * 24 * 1000 && cutTime <= 60L * 60 * 24 * 30 * 1000) {
                result = ((int) (cutTime / 60 / 60 / 1000 / 24)) + "天前";
            } else if (cutTime > 60L * 60 * 24 * 30 * 1000 && cutTime <= 60L * 60 * 24 * 30 * 1000 * 12) {
                result = ((int) (cutTime / 60 / 60 / 1000 / 24 / 30)) + "个月前";
            } else {
                result = date;
            }

        } catch (ParseException e) {
//            e.printStackTrace();
            Logger.d("getTextMMDD 日期格式化错误 " + date);
        }
        return result;
    }
}
